﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogicLayer;

namespace PresentationLayer
{
    public partial class frmLogin : Form
    {
        #region Properties
        private frmMain MdiForm { get; set; }
        #endregion

        #region Constructors
        public frmLogin(frmMain pMdiForm)
        {
            InitializeComponent();
            this.MdiForm = pMdiForm;
        }
        #endregion

        #region Events
        private void frmLogin_Load(object sender, EventArgs e)
        {
            this.MdiForm.Event_OnTranslationChanged += new frmMain.OnTranslationChangedHandler(Translate);
            Translate();
            FillComboBoxUsers();
            FillComboBoxMethod();
            SetFieldsToLoginMethod();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (Login())
            {
                Save();
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cboUsername_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetFieldsToState();
        }
        #endregion

        #region Public methods
        #endregion

        #region Private methods
        /// <summary>
        /// Translates every visible element.
        /// </summary>
        private void Translate()
        {
            this.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, "Text", this.Text, 0);
            grpDetails.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, grpDetails.Name, grpDetails.Text, 0);
            lblUsername.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblUsername.Name, lblUsername.Text, 0);
            lblPassword.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblPassword.Name, lblPassword.Text, 0);
            lblMethod.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, lblMethod.Name, lblMethod.Text, 0);
            grpOptions.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, grpOptions.Name, grpOptions.Text, 0);
            btnCancel.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, btnCancel.Name, btnCancel.Text, 0);
            btnLogin.Text = this.MdiForm.DataAccess.GetTranslation(this.Name, btnLogin.Name, btnLogin.Text, 0);
        }

        /// <summary>
        /// Gets the selected user.
        /// </summary>
        /// <returns>The selected user.</returns>
        private clsUser GetUser()
        {
            clsUser User = (clsUser)cboUsername.SelectedItem;
            return User;
        }

        /// <summary>
        /// Login the user.
        /// </summary>
        private Boolean Login()
        {
            if (CheckUserPassword())
            {
                this.MdiForm.LoginUser(GetUser());
                return true;
            }
            else
            {
                MessageBox.Show("Password incorrect.");
                txtPassword.Text = "";
                txtPassword.Focus();
                return false;
            }
        }

        /// <summary>
        /// Saves the login settings.
        /// </summary>
        private void Save()
        {
            if (cboMethod.SelectedIndex == 0)
            {
                this.MdiForm.DataAccess.Settings.SaveLoginSettings(clsSettings.enumLoginMethod.AskForPassword);
            }
            else
            {
                this.MdiForm.DataAccess.Settings.SaveLoginSettings(clsSettings.enumLoginMethod.LoginAutomatically);
            }
        }

        /// <summary>
        /// Sets the fields to the state of user inputs.
        /// </summary>
        private void SetFieldsToState()
        {
            btnLogin.Enabled = CheckInputs();
            txtPassword.Enabled = CheckInputs();
            cboMethod.Enabled = CheckInputs();
        }

        /// <summary>
        /// Checks if inputs are given.
        /// </summary>
        /// <returns></returns>
        private Boolean CheckInputs()
        {
            if (cboUsername.SelectedIndex == -1)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Checks the password inserted by the user.
        /// </summary>
        /// <returns></returns>
        private Boolean CheckUserPassword()
        {
            clsUser User = GetUser();
            if (txtPassword.Text == User.Password)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Gets all users from the database and fills the user ComboBox with them.
        /// </summary>
        private void FillComboBoxUsers()
        {
            if (cboUsername.Items.Count > 0)
            {
                cboUsername.Items.Clear();
            }

            ArrayList Users = this.MdiForm.DataAccess.GetAllUsers();
            foreach (clsUser User in Users)
            {
                cboUsername.Items.Add(User);
            }

            SetFieldsToState();
        }

        private void SetFieldsToLoginMethod()
        {
            if (this.MdiForm.DataAccess.Settings.User_Login != null)
            {
                if (this.MdiForm.DataAccess.Settings.User_Login.ID > 0)
                {
                    for (Int32 i = 0; i < cboUsername.Items.Count; i++)
                    {
                        clsUser User = (clsUser)cboUsername.Items[i];
                        if (User.ID == this.MdiForm.DataAccess.Settings.User_Login.ID)
                        {
                            cboUsername.SelectedIndex = i;
                            break;
                        }
                    }

                    //If changes are made here in the read section, changes have to be made in the fill section, too.
                    cboMethod.SelectedIndex = 1;
                }

                if (this.MdiForm.DataAccess.Settings.User_Login.Password != "")
                {
                    txtPassword.Text = this.MdiForm.DataAccess.Settings.User_Login.Password;
                }

                //Only login if possible.
                if (cboUsername.Items.Count > 0)
                {
                    if (Login())
                    {
                        this.Close();
                    }
                }
            }
        }

        /// <summary>
        /// Fills the method ComboBox with options.
        /// </summary>
        private void FillComboBoxMethod()
        {
            if (cboMethod.Items.Count > 0)
            {
                cboMethod.Items.Clear();
            }

            //If changes are made here in the fill section, changes have to be made in the read section, too.
            cboMethod.Items.Add(new clsItem(this.MdiForm.DataAccess.GetTranslation(this.Name, cboMethod.Name + "_Item1", "Ask for password", 0), 1));
            cboMethod.Items.Add(new clsItem(this.MdiForm.DataAccess.GetTranslation(this.Name, cboMethod.Name + "_Item2", "Login automatically", 0), 2));

            cboMethod.SelectedIndex = 0;
        }
        #endregion
    }
}
